package com.dd.admin.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.common.convert.SysNoticeConvert;
import com.dd.common.dto.system.notice.SysNoticeDTO;
import com.dd.common.dto.system.notice.SysNoticeQueryDTO;
import com.dd.common.entity.SysNotice;
import com.dd.common.pojo.dto.PageDTO;
import com.dd.common.pojo.dto.group.CreateGroup;
import com.dd.common.pojo.dto.group.UpdateGroup;
import com.dd.common.service.SysNoticeService;
import com.dd.common.utils.AjaxResult;
import com.dd.common.utils.PageResult;
import com.dd.common.validator.annotation.IDMust;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@SaIgnore
@Tag(name = "系统公告管理")
@Validated
@RequestMapping("/sys/notice")
@RestController
public class SysNoticeController {
    @Resource
    private SysNoticeService sysNoticeService;

    @Resource
    private SysNoticeConvert sysNoticeConvert;
    @Operation(summary = "系统公告添加", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "系统公告数据",
            content = @Content(
                    mediaType = MediaType.APPLICATION_JSON_VALUE,
                    schema = @Schema(implementation = SysNoticeDTO.class)
            )
    ))
    @PostMapping("/add")
    public AjaxResult<Boolean> add(@Validated(CreateGroup.class) @RequestBody SysNoticeDTO dto) {
        SysNotice sysNotice = sysNoticeConvert.dtoToEntity(dto);
        sysNotice.setPublisherId(StpUtil.getLoginIdAsLong());
        return AjaxResult.success(sysNoticeService.save(sysNotice));
    }

    @Operation(summary = "系统公告编辑", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "系统公告数据",
            content = @Content(
                    mediaType = MediaType.APPLICATION_JSON_VALUE,
                    schema = @Schema(implementation = SysNoticeDTO.class)
            )
    ))
    @PatchMapping("/edit")
    public AjaxResult<Boolean> edit(@Validated(UpdateGroup.class) @RequestBody SysNoticeDTO dto) {
        return AjaxResult.success(sysNoticeService.updateById(sysNoticeConvert.dtoToEntity(dto)));
    }
    @Operation(summary = "系统公告列表")
    @GetMapping("/list")
    public AjaxResult<List<SysNotice>> list(@Validated SysNoticeQueryDTO dto) {
        LambdaQueryWrapper<SysNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getStatus() != null, SysNotice::getStatus, dto.getStatus())
                .eq(dto.getNoticeType() != null, SysNotice::getNoticeType, dto.getNoticeType())
                .like(StringUtils.hasText(dto.getNoticeTitle()), SysNotice::getNoticeTitle, dto.getNoticeTitle())
                .like(StringUtils.hasText(dto.getNoticeContent()), SysNotice::getNoticeContent, dto.getNoticeContent())
                .like(StringUtils.hasText(dto.getPublisherName()), SysNotice::getPublisherName, dto.getPublisherName())
                .ge(dto.getPublishTimeStart() != null, SysNotice::getPublishTime, dto.getPublishTimeStart())
                .le(dto.getPublishTimeEnd() != null, SysNotice::getPublishTime, dto.getPublishTimeEnd())
                .ge(dto.getExpireTimeStart() != null, SysNotice::getExpireTime, dto.getExpireTimeStart())
                .le(dto.getExpireTimeEnd() != null, SysNotice::getExpireTime, dto.getExpireTimeEnd())
                .orderByDesc(SysNotice::getId);
        return AjaxResult.success(sysNoticeService.list(queryWrapper));
    }

    @Operation(summary = "系统公告分页列表")
    @GetMapping("/page")
    public AjaxResult<PageResult<SysNotice>> page(@Validated PageDTO pageDTO, @Validated SysNoticeQueryDTO dto) {
        Page<SysNotice> page = Page.of(pageDTO.getCurrent(), pageDTO.getSize());
        LambdaQueryWrapper<SysNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getStatus() != null, SysNotice::getStatus, dto.getStatus())
                .eq(dto.getNoticeType() != null, SysNotice::getNoticeType, dto.getNoticeType())
                .like(StringUtils.hasText(dto.getNoticeTitle()), SysNotice::getNoticeTitle, dto.getNoticeTitle())
                .like(StringUtils.hasText(dto.getNoticeContent()), SysNotice::getNoticeContent, dto.getNoticeContent())
                .like(StringUtils.hasText(dto.getPublisherName()), SysNotice::getPublisherName, dto.getPublisherName())
                .ge(dto.getPublishTimeStart() != null, SysNotice::getPublishTime, dto.getPublishTimeStart())
                .le(dto.getPublishTimeEnd() != null, SysNotice::getPublishTime, dto.getPublishTimeEnd())
                .ge(dto.getExpireTimeStart() != null, SysNotice::getExpireTime, dto.getExpireTimeStart())
                .le(dto.getExpireTimeEnd() != null, SysNotice::getExpireTime, dto.getExpireTimeEnd())
                .orderByDesc(SysNotice::getId);
        IPage<SysNotice> result = sysNoticeService.page(page, queryWrapper);
        return AjaxResult.success(PageResult.iPageHandle(result.getTotal(), result.getCurrent(), result.getSize(), result.getRecords()));
    }

    @Operation(summary = "系统公告详情", parameters = @Parameter( name = "id", description = "系统公告ID", in = ParameterIn.PATH))
    @GetMapping("/detail/{id}")
    public AjaxResult<SysNotice> detail(@PathVariable @IDMust Long id) {
        return AjaxResult.success(sysNoticeService.getById(id));
    }

    @Operation(summary = "系统公告删除", parameters = @Parameter( name = "id", description = "系统公告ID", in = ParameterIn.PATH))
    @DeleteMapping("/delete/{id}")
    public AjaxResult<Boolean> delete(@PathVariable @IDMust Long id) {
        return AjaxResult.success(sysNoticeService.removeById(id));
    }
}
